﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace TPCN.DAL
{
   public class ChiTietHoaDonBanDAL
    {
        public static ChiTietHoaDonBanObj GetByID(string ChitiethoadonbanID)
        {
            ChiTietHoaDonBanObj obj = new ChiTietHoaDonBanObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbChiTietHoaDonBan_GetByID";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ChitiethoadonbanID", ChitiethoadonbanID);
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        dr.Read();
                        obj = obj.SetValuesFromDataReader(dr);
                    }
                    dr.Close();
                }
            }
            return obj;
        }

        public static List<ChiTietHoaDonBanObj> GetByAll()
        {
            List<ChiTietHoaDonBanObj> result = new List<ChiTietHoaDonBanObj>();
            ChiTietHoaDonBanObj obj = new ChiTietHoaDonBanObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbChiTietHoaDonBan_GetByAll";
                cmd.CommandType = CommandType.StoredProcedure;
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            result.Add(obj.SetValuesFromDataReader(dr));
                        }
                    }
                    dr.Close();
                    obj = null;
                }
            }
            return result;
        }

        public static DataTable TinhDoanhThuTheoSanPham(DateTime startdate, DateTime enddate, string HangID)
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbChiTietHoaDonBan_TinhDoanhThuTheoSanPham";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@start", startdate);
                cmd.Parameters.AddWithValue("@end", enddate);
                cmd.Parameters.AddWithValue("@productId", HangID);
                return Database.db.GetData(cmd);
            }
        }

        public static List<ChiTietHoaDonBanObj> GetByHoadonbanID(string HoadonbanID)
        {
            List<ChiTietHoaDonBanObj> result = new List<ChiTietHoaDonBanObj>();
            ChiTietHoaDonBanObj obj = new ChiTietHoaDonBanObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbChiTietHoaDonBan_GetByHoaDonBanID";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@HoadonbanID", HoadonbanID);
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            result.Add(obj.SetValuesFromDataReader(dr));
                        }
                    }
                    dr.Close();
                    obj = null;
                }
            }
            return result;
        }

        public static bool Insert(ChiTietHoaDonBanObj obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbChiTietHoaDonBan_Insert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@HoadonbanID", (obj.HoadonbanID == null) ? String.Empty : obj.HoadonbanID);
                    cmd.Parameters.AddWithValue("@HangID", (obj.HangID == null) ? String.Empty : obj.HangID);
                    cmd.Parameters.AddWithValue("@Soluong", (obj.Soluong == null) ? "0" : obj.Soluong);
                    cmd.Parameters.AddWithValue("@Dongia", (obj.Dongia == null) ? "0" : obj.Dongia);
                    cmd.Parameters.AddWithValue("@Ghichu", (obj.Ghichu == null) ? String.Empty : obj.Ghichu);
                    cmd.Parameters.AddWithValue("@Chietkhau", (obj.Chietkhau == null) ? "0" : obj.Chietkhau);
                    cmd.Parameters.AddWithValue("@Hinhthucchietkhau", (obj.Hinhthucchietkhau == null) ? "0" : obj.Hinhthucchietkhau);
                    cmd.Parameters.AddWithValue("@Noidungchietkhau", (obj.Noidungchietkhau == null) ? String.Empty : obj.Noidungchietkhau);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool Update(ChiTietHoaDonBanObj obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbChiTietHoaDonBan_Update";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ChitiethoadonbanID", (obj.ChitiethoadonbanID == null) ? String.Empty : obj.ChitiethoadonbanID);
                    cmd.Parameters.AddWithValue("@HoadonbanID", (obj.HoadonbanID == null) ? String.Empty : obj.HoadonbanID);
                    cmd.Parameters.AddWithValue("@HangID", (obj.HangID == null) ? String.Empty : obj.HangID);
                    cmd.Parameters.AddWithValue("@Soluong", (obj.Soluong == null) ? "0" : obj.Soluong);
                    cmd.Parameters.AddWithValue("@Dongia", (obj.Dongia == null) ? "0" : obj.Dongia);
                    cmd.Parameters.AddWithValue("@Ghichu", (obj.Ghichu == null) ? String.Empty : obj.Ghichu);
                    cmd.Parameters.AddWithValue("@Chietkhau", (obj.Chietkhau == null) ? "0" : obj.Chietkhau);
                    cmd.Parameters.AddWithValue("@Hinhthucchietkhau", (obj.Hinhthucchietkhau == null) ? "0" : obj.Hinhthucchietkhau);
                    cmd.Parameters.AddWithValue("@Noidungchietkhau", (obj.Noidungchietkhau == null) ? String.Empty : obj.Noidungchietkhau);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool Delete(string ChitiethoadonbanID)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "sp_tbChiTietHoaDonBan_DeleteByID";
                    cmd.Parameters.AddWithValue("@ChitiethoadonbanID", ChitiethoadonbanID);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        
        public static bool DeleteByHoadonbanID(string HoadonbanID)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "sp_tbChiTietHoaDonBan_DeleteByHoaDonBanID";
                    cmd.Parameters.AddWithValue("@HoadonbanID", HoadonbanID);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
